import {
  hint,
  onlyChangeRouter,
  onlyChangeComplete,
  whetherLogin,
  encryptByDESModeEBC,
  decryptByDESModeEBC,
  testjiami,
} from "../../utils/util";
const app = getApp();
import User from "../../models/user";
const user = new User();

import Apipublic from "../../models/api";
const apipublic = new Apipublic();

Page({
  /**
   * 页面的初始数据
   */
  data: {
    token: "",
    baseImgUrl: "",
    focusOn: false,
    list: [
      {
        text: "邀约访客",
        url: "../../static/icon/i1.png",
        // router: "/owner-app/pages/subscribe/index",   //不要删之前的邀约流程
        router: "/owner-app/pages/subscribe-new/index",
      },
      {
        text: "一键报警",
        url: "../../static/icon/i2.png",
        router: "/owner-app/pages/alarm/index",
      },
      {
        text: "物联网设备",
        url: "../../static/icon/i3.png",
        router: "/owner-app/pages/device/index",
      },
      {
        text: "出租屋管理",
        url: "../../static/icon/i4.png",
        // router: "/owner-app/pages/rent-house/index",   //不要删  后续这个功能要放开
      },
      {
        text: "宠物管理",
        url: "../../static/icon/i5.png",
        router: "/owner-app/pages/pet/index",
      },
      {
        text: "生活服务",
        url: "../../static/icon/i6.png",
      },
      {
        text: "政务服务",
        url: "../../static/icon/i7.png",
        // router: "/owner-app/pages/gover-serve/index",   //不要删  后续这个功能要放开
      },
      {
        text: "物业服务",
        url: "../../static/icon/i8.png",
        router: "/owner-app/pages/property/index",
      },
      // {
      //   text: "信息采集",
      //   url: "../../static/icon/ic_cj.png",
      //   router: "/owner-app/pages/userinfo/index",
      // },
    ],
    show: false,
    communityList: [],
    //当前选中的社区
    selectedIndex: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    // console.log(decryptByDESModeEBC(encryptByDESModeEBC()))
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let stateFocusOn = wx.getStorageSync("focusOn");
    let scene = wx.getStorageSync("scene");
    if (!stateFocusOn.toString().length) {
      if (
        scene == "1011" ||
        scene == "1017" ||
        scene == "1025" ||
        scene == "1047" ||
        scene == "1124" ||
        scene == "1001" ||
        scene == "1038" ||
        scene == "1041" ||
        scene == "1089" ||
        scene == "1090" ||
        scene == "1104" ||
        scene == "1131" ||
        scene == "1187"
      ) {
        stateFocusOn = true;
      }
    }

    var { baseImgUrl } = getApp().globalImgUrl.config;
    this.setData({
      baseImgUrl,
      focusOn: stateFocusOn,
      token: wx.getStorageSync("token"),
    });

    this.getTabBar().init();
    this.initPageData();
    // 打印解码后的字符串

    apipublic.dictItemList("audit_on").then((res) => {
      wx.setStorageSync("paiNew", res.result[0].value == "0" ? false : true); //0  不展示    1展示
    });
  },
  onlyChangeRouter,
  onlyChangeComplete,
  whetherLogin,
  focusHandle() {
    this.setData({
      focusOn: false,
    });
    wx.setStorageSync("focusOn", this.data.focusOn);
  },
  // 根据所选社区来缓存版本号和code
  saveVersionAndCode(selectedIndex) {
    let data = this.data.communityList[selectedIndex];
    wx.setStorageSync("qrCodeVersion", data.version);
    wx.setStorageSync("commid", data.commId); //小区id
    wx.setStorageSync("commName", data.commName); //小区名字
    this.getCommgates(data.commId);
  },
  qrCodeOpenDoor() {
    let token = wx.getStorageSync("token");
    if (!token) {
      this.whetherLogin(token);
      return;
    }

    this.onlyChangeComplete();
    if (this.data.communityList.length === 0) {
      hint("暂无社区信息！");
      return;
    }
    let data = this.data.communityList[this.data.selectedIndex];
    // this.getCommgates(data.commid);
    // // wx.removeStorageSync('qrCodeVersion')
    // wx.setStorageSync('qrCodeVersion', data.version)

    let user = wx.getStorageSync("user");
    wx.navigateTo({
      url: `/owner-app/pages/qrcode/index?id=${user.id}&phone=${user.phone}&commid=${data.commId}`,
    });
  },
  listHandle(e) {
    let token = wx.getStorageSync("token");
    if (!token) {
      this.whetherLogin(token);
      return;
    }

    this.onlyChangeRouter(e);
  },
  selectCommunity() {
    let token = wx.getStorageSync("token");
    if (!token) {
      this.whetherLogin(token);
      return;
    }

    this.setData({
      show: true,
    });
  },
  onClose() {
    this.setData({ show: false });
  },
  // 获取单元门列表
  getCommgates(commid) {
    // wx.showLoading({
    //   title: "加载中",
    //   mask: true,
    // });
    // user.getCommgates(commid).then((res) => {
    //   wx.hideLoading();
    //   let { result } = res;
    //   if (result.length > 0) {
    //     let code = result.map((item) => {
    //       if (!(item.code instanceof String)) {
    //         item.code += "";
    //       }
    //       return item.code;
    //     });
    //     wx.setStorageSync("code", code);
    //   }
    // });
  },
  selectedItem(event) {
    var index = event.currentTarget.dataset["index"];
    this.setData({
      selectedIndex: index,
      show: false,
    });
    //获取版本号和code
    this.saveVersionAndCode(index);
  },
  initPageData() {
    let memberId = wx.getStorageSync("user").memberId;
    if (!memberId) {
      return;
    }

    user
      .centerCellSelection()
      .then((res) => {
        if (res.result.comm && res.result.comm.length > 0) {
          let index = res.result.comm.findIndex((v) => {
            return v.status == 1;
          });
          this.setData({
            selectedIndex: index,
          });

          let communityList = res.result.comm;
          this.setData({
            communityList,
          });
          if (this.data.communityList.length > 0) {
            // 获取版本获取code
            this.saveVersionAndCode(this.data.selectedIndex);
          }
        }
      })
      .catch((err) => {
        console.log(err);
      });
  },
  onShareAppMessage: function () {
    return {
      title: "标准化智慧社区", //转发的标题。当前小程序名称
      path: `/pages/home/index`, //转发的路径
      imageUrl: "../../static/icon/share.jpg", //自定义图片路径 支持PNG及JPG。显示图片长宽比是 5:4。
    };
  },
});
